package org.zhaolei.controller;


import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zhaolei.request.LockCouponRecordRequest;
import org.zhaolei.service.CouponRecordService;
import org.zhaolei.util.JsonData;
import org.zhaolei.vo.CouponRecordVO;
import springfox.documentation.spring.web.json.Json;

import java.util.Map;

@RestController
@RequestMapping("/api/coupon_record/v1")
public class CouponRecordController {

    @Autowired
    private CouponRecordService couponRecordService;

    @ApiOperation("分页查询个人优惠券")
    @GetMapping("page")
    public JsonData page(@ApiParam(value = "当前页") @RequestParam(value = "page", defaultValue = "1") int page,
                         @ApiParam(value = "每页显示条数") @RequestParam(value = "size", defaultValue = "10") int size) {
        Map<String, Object> pageResult = couponRecordService.page(page, size);

        return JsonData.buildSuccess(pageResult);
    }

    @ApiOperation("查询个人优惠券详情")
    @GetMapping("detail/{record_id}")
    public JsonData getCouponRecordDetail(@ApiParam(value = "优惠券记录id") @PathVariable("record_id") long couponRecordId) {
        CouponRecordVO couponRecordVO = couponRecordService.getCouponRecordDetail(couponRecordId);

        return JsonData.buildSuccess(couponRecordVO);
    }

    @ApiOperation("rpc-锁定，优惠券记录")
    @PostMapping("lock_records")
    public JsonData lockCouponRecords(@ApiParam("锁定优惠券请求对象") @RequestBody LockCouponRecordRequest lockCouponRecordRequest) {

        JsonData jsonData = couponRecordService.lockCouponRecords(lockCouponRecordRequest);

        return JsonData.buildSuccess(jsonData);
    }
}
